scan-rules
This crate provides some macros for quickly parsing values out of text. Roughly speaking, it does the inverse of the print!
/format!
macros; or, in other words, a similar job to scanf
from C.
The macros of interest are:
readln!
- reads and scans a line from standard input.try_readln!
- likereadln!
, except it returns aResult
instead of panicking.scan!
- scans the provided string.
Plus two convenience macros:
let_scan!
- scans a string and binds captured values directly to local variables. Only supports one pattern and panics if it doesn't match.let_scanln!
- reads and scans a line from standard input, binding captured values directly to local variables. Only supports one pattern and panics if it doesn't match.
If you are interested in implementing support for your own types, see the ScanFromStr
trait.
The available abstract scanners can be found in the scanner
module.
Links
Compatibility
scan-rules
is compatible with rustc
version 1.6.0 and higher.
-
Due to a breaking change,
scan-rules
is not compatible withregex
version 0.1.66 or higher. -
rustc
< 1.10 will not have thelet_scanln!
macro. -
rustc
< 1.7 will have only concrete implementations ofScanFromStr
for theEverything
,Ident
,Line
,NonSpace
,Number
,Word
, andWordish
scanners for&str
andString
output types. 1.7 and higher will have generic implementations for all output types such that&str: Into<Output>
. -
rustc
< 1.6 is explicitly not supported, due to breaking changes in Rust itself.
Quick Examples
Here is a simple CLI program that asks the user their name and age. You can run this using cargo run --example ask_age
.
extern crate scan_rules;
use Word;
This example shows how to parse one of several different syntaxes. You can run this using cargo run --example scan_data
.
extern crate scan_rules;
use BTreeSet;
// `Word` is an "abstract" scanner; rather than scanning itself, it scans some
// *other* type using custom rules. In this case, it scans a word into a
// string slice. You can use `Word<String>` to get an owned string.
use Word;
This example demonstrates using runtime scanners and the let_scan!
convenience macro. You can run this using cargo run --example runtime_scanners
.
//! **NOTE**: requires the `regex` feature.
extern crate scan_rules;
License
Licensed under either of
- MIT license (see LICENSE or http://opensource.org/licenses/MIT)
- Apache License, Version 2.0 (see LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you shall be dual licensed as above, without any additional terms or conditions.